package com.awifi.demo.netty.nio;


import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description: ToDo
 * @author: marin
 * @time: 2020/9/2 16:14
 */
public class NIOFileChannel {
    public static void main(String[] args) throws IOException {

        readFile();
    }

    public static void writeFile() throws IOException {

        String str = "hello,亮";
        FileOutputStream fileOutputStream = new FileOutputStream("hello.txt");

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes());
        byteBuffer.flip();

        FileChannel fileChannel = fileOutputStream.getChannel();
        fileChannel.write(byteBuffer);

        fileOutputStream.close();
    }

    public static void readFile() throws IOException {

        FileInputStream fileInputStream = new FileInputStream("hello.txt");

        FileChannel channel = fileInputStream.getChannel();


        ByteBuffer byteBuffer = ByteBuffer.allocate(60);
        channel.read(byteBuffer);


        FileOutputStream fileOutputStream = new FileOutputStream("hello1.txt");
        FileChannel channel1 = fileOutputStream.getChannel();
        byteBuffer.flip();

        channel1.write(byteBuffer);

        System.out.println(new String(byteBuffer.array()));

        fileInputStream.close();
        fileOutputStream.close();

    }


}
